// pages/address/index.js
var tcity = require("../../../../utils/citys.js");
import http from '../../../../service/http.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    radioItems: [
      { name: '先生', value: '先生' },
      { name: '女士', value: '女士' }
    ],
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false,
    id: 0
  },
  bindChange: function (e) {
    //console.log(e);
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;

    if (val[0] != t[0]) {
      console.log('province no ');
      const citys = [];
      const countys = [];

      for (var i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (var i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name)
      }

      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })
      console.log(this.data.province)
      return;
    }
    if (val[1] != t[1]) {
      console.log('city no');
      const countys = [];

      for (var i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
      }

      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0]
      })
      console.log(this.data.province)

      return;
    }
    if (val[2] != t[2]) {
      console.log('county no');
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      console.log(this.data.province)

      return;
    }


  },
  open: function () {
    this.setData({
      condition: !this.data.condition
    })
  },
  onLoad: function (options) {

    var that = this;
    var tjorder = options.tjorder ? options.tjorder : 0
    that.setData({
      tjorder: tjorder
    })
    tcity.init(that);

    var cityData = that.data.cityData;


    const provinces = [];
    const citys = [];
    const countys = [];

    for (var i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i].name);
    }
    console.log('省份完成');
    for (var i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name)
    }
    console.log('city完成');
    for (var i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }

    that.setData({
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
      'province': cityData[0].name,
      'city': cityData[0].sub[0].name,
      'county': cityData[0].sub[0].sub[0].name
    })
    console.log('初始化完成');
    if (options.id) {
      http.req('/Wxuser/adds_info', { id: options.id }, function (res) {
        if (res.code == '0') {
          console.log(res.data)
          if (res.data.sex == '先生') {
            var radioItems = [
              { name: '先生', value: '先生', checked: 'true' },
              { name: '女士', value: '女士' }
            ];
          } else if (res.data.sex == '女士') {
            var radioItems = [
              { name: '先生', value: '先生' },
              { name: '女士', value: '女士', checked: 'true' }
            ];
          }
          that.setData({
            addr: res.data,
            'province': res.data.sheng,
            'city': res.data.shi,
            'county': res.data.qu,
            radioItems: radioItems,
            id: options.id
          })
        }
      });
    }

  },
  formSubmit: function (e) {
    var that = this;

    ////console.log('form鍙戠敓浜唖ubmit浜嬩欢锛屾惡甯︽暟鎹负锛�', e.detail.value)
    if (!e.detail.value.name) {

      wx.showModal({
        title: '温馨提示',
        content: '请填写姓名',
        showCancel: false

      })
      return false;
    }
    if (!e.detail.value.tel) {

      wx.showModal({
        title: '温馨提示',
        content: '请填写电话',
        showCancel: false

      })
      return false;
    }
    if (!e.detail.value.dizhi) {

      wx.showModal({
        title: '温馨提示',
        content: '请填写详细地址',
        showCancel: false

      })
      return false;
    }
    var user = wx.getStorageSync('user')
    // var formdata = e.detail.value;
    var formdata = {
      id: that.data.id,
      userid: user.userid,
      name: e.detail.value.name,
      tel: e.detail.value.tel,
      adds: e.detail.value.dizhi,
      shengshiqu: e.detail.value.shengshiqu,
      sheng: that.data.province,
      shi: that.data.city,
      qu: that.data.county,
      sex: e.detail.value.sex
    }

    http.req('/Wxuser/add_adds', formdata, function (res) {
      console.log(res.status)

      if (res.status == '1') {
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000,
          success: function () {
            if (that.data.tjorder == '1') {
              wx.navigateBack({
                delta: 1
              })
            } else {
              wx.redirectTo({
                url: '/pages/shop/address/list/list'
              })
            }

          }
        })


      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      }
    });

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindTextAreaBlur: function (e) {
    console.log(e.detail.value)
  },


})